<template>
	<view class="my-settle-container">
		<label class="radio" @click="changeAllChecked">
			<radio :checked='isFullCheck' color="#C00000" /><text>全选</text>
		</label>
		<view class="amount-box">
			合计: <text>￥{{checkedPriceCount}}</text>
		</view>
		<view class="settlement" @click="settlementBtn">
			结算 ({{checkedEvery}})
		</view>
	</view>
</template>

<script>
	import {
		mapGetters,
		mapMutations,
		mapState
	} from "vuex"
	import badgeMixin from '@/mixins/tabber-badge.js'
	export default {
		mixins: [badgeMixin],
		name: "my-settle",
		data() {
			return {
				seconds: 3,
				timer: null
			};
		},
		computed: {
			...mapGetters('my_cart', ['checkedEvery', "total", "checkedPriceCount"]),
			...mapState('my_address', ['address']),
			...mapState('my_address', ['token']),
			...mapGetters('my_address', ['addstr']),
			...mapState('my_cart', ['cart']),
			isFullCheck() {
				return this.total === this.checkedEvery
			}
		},
		methods: {
			...mapMutations("my_cart", ["updateAllcheckState"]),
			...mapMutations("my_address", ["updateRedirectInfo"]),
			changeAllChecked() {
				this.updateAllcheckState(!this.isFullCheck)
			},
			settlementBtn() {
				// console.log(this.checkedEvery);
				// console.log(this.address);
				// console.log(this.token);
				if (!this.checkedEvery) return uni.$showMsg('请勾选需要购买的商品')
				console.log(this.address);
				if (!this.address.cityName || !this.address.provinceName) return uni.$showMsg('请选择收货地址')
				if (!this.token) return this.delayNavigate()
				this.payOrder()
			},
			async payOrder() {
				// 创建订单
				const orderInfo = {
					// order_price:this.checkedPriceCount,
					order_price: 0.01,
					consignee_addr: this.addstr,
					goods: this.cart.filter(x => x.goods_state).map(item => ({
						goods_id: item.goods_id,
						goods_number: item.goods_count,
						goods_price: item.goods_price
					}))
				}
				const {
					data: res
				} = await uni.$http.post('/api/public/v1/my/orders/create', orderInfo)
				if(res.meta.status !== 200) return uni.$showMsg('创建订单失败')
				const orderNumber = res.message.order_number
				const {data:res2} = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder')
				if(res2.meta.status !== 200) return uni.$showMsg('预付订单生成失败！')
				const payInfo = res2.message.pay
				// console.log(payInfo);
				const resPay = await uni.requestPayment(payInfo)
				const {data:res3}=await uni.$http.post('/api/public/v1/my/orders/chkOrder',{order_number:orderNumber})
				if(res3.meta.status !== 200) return uni.$showMsg('订单未支付！')
				uni.showToast({
					title:'支付完成！',
					icon:'success'
				})
			},
			delayNavigate() {
				this.seconds = 3
				this.showTips(this.seconds)
				this.timer = setInterval(() => {
					this.seconds--
					if (this.seconds <= 0) {
						uni.switchTab({
							url: '/pages/my/my',
							success: () => {
								this.updateRedirectInfo({
									openType: 'switchTab',
									from: '/pages/cart/cart'
								})
							}
						})
						clearInterval(this.timer)
						return
					}
					this.showTips(this.seconds)
				}, 1000)
			},
			showTips(n) {
				uni.showToast({
					icon: "none",
					title: '请登录后再结算' + n + '秒之后自动转跳到登录页',
					mask: true,
					duration: 1500
				})
			}
		}
	}
</script>

<style lang="scss">
	.my-settle-container {
		display: flex;
		justify-content: space-between;
		align-items: center;
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 50px;
		padding-left: 5px;
		font-size: 14px;

		.radio {
			display: flex;
			align-items: center;
		}

		.amount-box {
			text {
				color: #C00000;
			}
		}

		.settlement {
			height: 50px;
			background-color: #C00000;
			color: #fff;
			padding: 0 10px;
			line-height: 50px;
			min-width: 100px;
			text-align: center;
		}
	}
</style>